package com.chaowen.yixin;

import java.util.Arrays;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.chaowen.config.Config;
import com.chaowen.model.UserInfo;
import com.chaowen.service.TaskContext;

public class Fragment_answer extends Fragment_super implements
		android.widget.CompoundButton.OnCheckedChangeListener {
	//控件
	public static TextView answerNmuber;
	public static RadioGroup radioGroup;
	public static LinearLayout multipleGroup;
	public static LinearLayout digitalGroup;
	public static Button answerBtn;
	private RadioButton E_A;
	private RadioButton E_B;
	private RadioButton E_C;
	private RadioButton E_D;
	private RadioButton E_E;
	private RadioButton E_F;
	private CheckBox E_A1;
	private CheckBox E_B1;
	private CheckBox E_C1;
	private CheckBox E_D1;
	private CheckBox E_E1;
	private CheckBox E_F1;
	private CheckBox E_A11;
	private CheckBox E_B11;
	private CheckBox E_C11;
	private CheckBox E_D11;
	private CheckBox E_E11;
	private CheckBox E_F11;
	private CheckBox E_G11;
	private CheckBox E_H11;
	private CheckBox E_I11;
	private CheckBox E_J11;
	private Handler handler;

	public Fragment_answer(Handler handler) {
		this.handler = handler;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater
				.inflate(R.layout.fragment_answer, container, false);
		initView(view);
		initGroup();
		return view;
	}
	
	private void initGroup() {
		if(!UserInfo.isSign) {
			answerBtn.setVisibility(View.INVISIBLE);
			digitalGroup.setVisibility(View.INVISIBLE);
			multipleGroup.setVisibility(View.INVISIBLE);
			radioGroup.setVisibility(View.INVISIBLE);
			answerNmuber.setText("等待答题开始命令");
			return;
		}
		if(Config.startNumbers == 0) return;
		answerBtn.setVisibility(View.VISIBLE);
		answerBtn.setText("提交答案");
		if(Config.startNumbers<=Config.endNumbers) {
			System.out.println("singleStart题目编号："+Config.startNumbers+"   "+Config.singleStart+"   "+Config.singleEnd);
			System.out.println("multipleStart题目编号："+Config.startNumbers+"   "+Config.multipleStart+"   "+Config.multipleEnd);
			System.out.println("digitalStart题目编号："+Config.startNumbers+"   "+Config.digitalStart+"   "+Config.digitalEnd);
			if(Config.startNumbers>=Config.singleStart&&Config.startNumbers<=Config.singleEnd) {
				answerNmuber.setText(Config.paperName+"\r\n 单项选择题    题号："+Config.startNumbers);
				radioGroup.setVisibility(View.VISIBLE);
				multipleGroup.setVisibility(View.INVISIBLE);
				digitalGroup.setVisibility(View.INVISIBLE);
			}else if(Config.startNumbers>=Config.multipleStart&&Config.startNumbers<=Config.multipleEnd) {
				answerNmuber.setText(Config.paperName+"\r\n 多项选择题    题号："+Config.startNumbers);
				multipleGroup.setVisibility(View.VISIBLE);
				radioGroup.setVisibility(View.INVISIBLE);
				digitalGroup.setVisibility(View.INVISIBLE);
			}else if(Config.startNumbers>=Config.digitalStart&&Config.startNumbers<=Config.digitalEnd) {
				answerNmuber.setText(Config.paperName+"\r\n 数字题    题号："+Config.startNumbers);
				digitalGroup.setVisibility(View.VISIBLE);
				multipleGroup.setVisibility(View.INVISIBLE);
				radioGroup.setVisibility(View.INVISIBLE);
			}
		}else {
			answerNmuber.setText("您已经答完所有题目");
			answerBtn.setVisibility(View.INVISIBLE);
			digitalGroup.setVisibility(View.INVISIBLE);
			multipleGroup.setVisibility(View.INVISIBLE);
			radioGroup.setVisibility(View.INVISIBLE);
		}
	}

	private void initView(View view) {
		answerNmuber = (TextView) view.findViewById(R.id.answerNmuber);
		answerBtn = (Button) view.findViewById(R.id.answerBtn);
		radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
		multipleGroup = (LinearLayout)view.findViewById(R.id.multipleGroup);
		digitalGroup = (LinearLayout)view.findViewById(R.id.digitalGroup);
		// 单选按钮
		E_A = (RadioButton) view.findViewById(R.id.E_A);
		E_B = (RadioButton) view.findViewById(R.id.E_B);
		E_C = (RadioButton) view.findViewById(R.id.E_C);
		E_D = (RadioButton) view.findViewById(R.id.E_D);
		E_E = (RadioButton) view.findViewById(R.id.E_E);
		E_F = (RadioButton) view.findViewById(R.id.E_F);
		// 字母复选框
		E_A1 = (CheckBox) view.findViewById(R.id.E_A1);
		E_B1 = (CheckBox) view.findViewById(R.id.E_B1);
		E_C1 = (CheckBox) view.findViewById(R.id.E_C1);
		E_D1 = (CheckBox) view.findViewById(R.id.E_D1);
		E_E1 = (CheckBox) view.findViewById(R.id.E_E1);
		E_F1 = (CheckBox) view.findViewById(R.id.E_F1);
		// 数字复选框
		E_A11 = (CheckBox) view.findViewById(R.id.E_A11);
		E_B11 = (CheckBox) view.findViewById(R.id.E_B11);
		E_C11 = (CheckBox) view.findViewById(R.id.E_C11);
		E_D11 = (CheckBox) view.findViewById(R.id.E_D11);
		E_E11 = (CheckBox) view.findViewById(R.id.E_E11);
		E_F11 = (CheckBox) view.findViewById(R.id.E_F11);
		E_G11 = (CheckBox) view.findViewById(R.id.E_G11);
		E_H11 = (CheckBox) view.findViewById(R.id.E_H11);
		E_I11 = (CheckBox) view.findViewById(R.id.E_I11);
		E_J11 = (CheckBox) view.findViewById(R.id.E_J11);

		answerBtn.setOnClickListener(this);
		radioGroup.setOnCheckedChangeListener(new MyRadioGroupListener());
		
		E_A1.setOnCheckedChangeListener(this);
		E_B1.setOnCheckedChangeListener(this);
		E_C1.setOnCheckedChangeListener(this);
		E_D1.setOnCheckedChangeListener(this);
		E_E1.setOnCheckedChangeListener(this);
		E_F1.setOnCheckedChangeListener(this);
			
		E_A11.setOnCheckedChangeListener(this);
		E_B11.setOnCheckedChangeListener(this);
		E_C11.setOnCheckedChangeListener(this);
		E_D11.setOnCheckedChangeListener(this);
		E_E11.setOnCheckedChangeListener(this);
		E_F11.setOnCheckedChangeListener(this);
		E_G11.setOnCheckedChangeListener(this);
		E_H11.setOnCheckedChangeListener(this);
		E_I11.setOnCheckedChangeListener(this);
		E_J11.setOnCheckedChangeListener(this);

	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		String replaced = buttonView.getText().toString();
		if(isChecked) {
			if(!UserInfo.answer.contains(buttonView.getText())) {
				UserInfo.answer += replaced;
			}
		}else {
			if(UserInfo.answer.contains(buttonView.getText())) {
				UserInfo.answer = UserInfo.answer.replaceAll(replaced, "");
			}
		}
		char[] data = UserInfo.answer.toCharArray();
		Arrays.sort(data);
		UserInfo.answer = String.valueOf(data);
	}

	class MyRadioGroupListener implements OnCheckedChangeListener {
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			setAnswer(checkedId);
		}
	}

	/**
	 * 根据选项设置用户答案
	 * 
	 * @param checkedId
	 */
	private void setAnswer(int checkedId) {
		if (checkedId == E_A.getId()) {
			UserInfo.answer = E_A.getText().toString();
		} else if (checkedId == E_B.getId()) {
			UserInfo.answer = E_B.getText().toString();
		} else if (checkedId == E_C.getId()) {
			UserInfo.answer = E_C.getText().toString();
		} else if (checkedId == E_D.getId()) {
			UserInfo.answer = E_D.getText().toString();
		} else if (checkedId == E_E.getId()) {
			UserInfo.answer = E_E.getText().toString();
		} else if (checkedId == E_F.getId()) {
			UserInfo.answer = E_F.getText().toString();
		}
	}

	@Override
	public void onClick(View view) {
		if (Config.isAnswer) {
			answer();
			radioGroup.clearCheck();
			E_A1.setChecked(false);
			E_B1.setChecked(false);
			E_C1.setChecked(false);
			E_D1.setChecked(false);
			E_E1.setChecked(false);
			E_F1.setChecked(false);
			E_A11.setChecked(false);
			E_B11.setChecked(false);
			E_C11.setChecked(false);
			E_D11.setChecked(false);
			E_E11.setChecked(false);
			E_F11.setChecked(false);
			E_G11.setChecked(false);
			E_H11.setChecked(false);
			E_I11.setChecked(false);
			E_J11.setChecked(false);
		} 
	}

	private void answer() {
		// 参数有：用户名、密码
		answerBtn.setText("正在答题中...");
//		new AnswerTask(this.handler).execute();
//		Task task = TaskFactory.createTask("answer");
//		task.setHandler(handler);
//		task.execute();
		new TaskContext("AnswerTask").doTask(handler);//策略模式
	}

}
